<template>
  <div class="tab-bar">
    <template v-for="(item, index) in tabbarData">
      <div class="tab-bar-item" :class="{ active: currentIndex === index }" @click="itemClick(item, index)">
        <!-- class：设置静态类名，类名在元素渲染后不会改变。 -->
        <!-- :class：设置动态类名，类名会根据组件的数据状态动态变化 -->
        <img :src="getAssetURL(currentIndex === index ? item.imageActive : item.image)" alt="">
        <span class="text">{{ item.text }}</span>
      </div>
    </template>
  </div>
</template>

<script setup>

import tabbarData from '@/assets/data/tabbar.js'
import { getAssetURL } from '@/utils/load_assets.js'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const currentIndex = ref(0)
const router = useRouter()
const itemClick = (item, index) => {
  currentIndex.value = index
  router.push(item.path)
}

</script>

<style lang="less" scoped>
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;

  border-top: 1px solid #f3f3f3;

  .tab-bar-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    // & 当前选择器的父选择器
    // &.active 等同于
    // .tab-bar-item.active
    &.active {
      color: var(--primary-color);
    }

    img {
      width: 32px;
    }

    .text {
      font-size: 12px;
    }
  }
}
</style>